<script type="text/javascript" src="/files/editors/tinymce402/tinymce.min.js"></script>
<script type="text/javascript" src="/files/editors/tinymce402/tinymce_config.js"></script>
<script>             
    <?php if($this->useMoxie):?>
        tinyMceConfig.plugins[0] += ' moxiemanager'; 
        tinyMceConfig.moxiemanager_rootpath = '<?php echo Myext::fixTinyMceUploadPath($uploadEditorPath)?>';
        tinyMceConfig.moxiemanager_extensions = 'jpg,png,gif';
        <?php endif?>
    tinymce.init(tinyMceConfig);
</script>
<style>
    #post-form .block_en{
        display: <?php echo $model->has_en ? 'block' : 'none';?>;
    }
</style>

<div class="grid_4">       
    <div class="da-panel">
        <div class="da-panel-header">
            <span class="da-panel-title">
                <img alt="" src="<?php echo $this->themeUrl; ?>/files/theme/images/icons/color/monitor.png">
                Post
            </span>
        </div>

        <div id="da-ex-tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
            <?php $this->renderPartial('_tabs', array('model' => $model))?>
            <div style="padding-bottom: 20px;">


                <?php $this->widget('admin.components.widgets.AlertWidget');?>

                <style>
                    .wrapper { 
                        position: relative;
                        width: 100%;
                    }
                    .scrolls { 
                        overflow-x: scroll;
                        overflow-y: hidden;
                        height: 90px;
                        white-space:nowrap
                    } 
                    .img-block{
                        display: inline-block;
                        text-align: center;
                    } 
                    .img-block img{
                        height: 50px;
                    }
                    #Post_has_en{
                        display: inline;
                        width: 0;
                        float: left;
                        margin-top: 8px;    
                    }
                    #label_has_en{
                        display: inline;
                        width: 200px;
                    }
                </style>     


                <?php $form=$this->beginWidget('CActiveForm', array(
                        'id'=>'post-form',
                        'enableClientValidation'=>true,
                        'clientOptions'=>array(
                            'validateOnSubmit'=>true,
                        ),
                        //'enableAjaxValidation'=>true,
                        'htmlOptions' => array('class' => 'da-form', 'enctype' => 'multipart/form-data')
                    )); ?>

                <?php //echo $form->errorSummary($model); ?>



                <div class="da-form-inline">
                    <div class="da-form-row" style="margin-top: 15px;">
                        <div class="da-form-col-4-8">
                            <div class="da-form-item large">
                                <?php echo $form->checkBox($model,'has_en',array('maxlength'=>120)); ?>
                                <?php echo $form->labelEx($model,'has_en', array('id' => 'label_has_en')); ?>
                            </div>
                        </div>
                    </div>



                    <div class="da-form-row" style="margin-top: 15px;">
                        <div class="da-form-col-6-8">
                            <?php echo $form->labelEx($model,'title'); ?>
                            <div class="da-form-item large">
                                <span class="formNote">(Title should be 50-64 chars) | <span id="title_char_count"></span> chars</span>
                                <?php echo $form->textField($model,'title',array('maxlength'=>120)); ?>
                                <?php echo $form->error($model,'title');?>
                            </div>
                        </div>
                    </div>
                    <div class="da-form-row block_en" style="margin-top: 15px;">
                        <div class="da-form-col-6-8">
                            <?php echo $form->labelEx($model,'title_en'); ?>
                            <div class="da-form-item large">
                                <span class="formNote">(Title should be 50-64 chars) | <span id="title_en_char_count"></span> chars</span>
                                <?php echo $form->textField($model,'title_en',array('maxlength'=>120)); ?>
                                <?php echo $form->error($model,'title_en');?>
                            </div>
                        </div>
                    </div>

                    <div class="da-form-row">
                        <div class="da-form-col-4-8">
                            <?php echo $form->labelEx($model,'cat_id'); ?>
                            <div class="da-form-item large">
                                <?php echo $form->dropDownList($model, 'cat_id', PostCat::model()->data); ?>
                                <?php echo $form->error($model,'cat_id');?>
                            </div>
                        </div>
                    </div>
                    <style>
                        .upload_method{
                            cursor: pointer;
                        }
                        .upload_method.selected{
                            text-decoration: underline;
                            font-weight: bold;
                        }
                        #image_file, #image_url{
                            display: none;
                        }
                    </style>
                    <script>
                        $(function(){
                            $("#a_url").click(function(){
                                $('#a_file').removeClass('selected');    
                                $('#a_url').addClass('selected');    
                                $('#image_url').show();    
                                $('#image_file').hide();    
                                $('#Post_upload_method').val('url');    
                            });
                            $("#a_file").click(function(){
                                $('#a_url').removeClass('selected');    
                                $('#a_file').addClass('selected');    
                                $('#image_file').show();    
                                $('#image_url').hide();
                                $('#Post_upload_method').val('file');    
                            });


                            $("#browse_file").change(function(evt){
                                var files = evt.target.files;
                                var f = files[0];

                                if(!f.type.match('image.*')) {
                                    alert('File không hợp lệ. Hãy chọn 1 file ảnh khác.');
                                    return false;   
                                }

                                var i = document.createElement('input');
                                if('multiple' in i){
                                    var reader = new FileReader();
                                    reader.readAsDataURL(f);
                                    reader.onload = (function(){
                                        return function(e){
                                            $('#img_url').hide(); 
                                            $('#img_file').attr('src', e.target.result).show(); 
                                        };
                                    })(f);
                                    $('#img_review').show();
                                }
                            });      

                            $("#Post_image_url").bind('change keyup blur', function(evt){
                                var method = $('#Post_upload_method').val();
                                var ext = $(this).val().split('.').pop().toLowerCase(); 
                                if(method == 'url' && $.inArray(ext, [ 'jpg', 'gif', 'png' ] >= 0)){
                                    $('#img_file').hide();
                                    $('#img_url').attr('src', $(this).val()).show();
                                    $('#img_review').show(); 

                                } 
                            });

                            <?php if($model->upload_method == 'file'):?>
                                $('#image_file').show();    
                                $('#image_url').hide(); 
                                $('#a_file').addClass('selected'); 
                                <?php else:?>
                                $('#image_url').show();    
                                $('#image_file').hide();
                                $('#a_url').addClass('selected');
                                <?php if($model->image_url):?>
                                    $('#img_url').attr('src', '<?php echo $model->image_url?>').show();
                                    <?php endif?> 
                            <?php endif?>
                        });
                    </script>
                    <div class="da-form-row">
                        <div class="da-form-col-4-8">
                            <label>Cover Image <span class="required">*</span> </label>
                            <div class="da-form-item large">
                                <div style="float: left;">
                                    <a id="a_file" class="upload_method">Từ máy tính</a> &nbsp;|&nbsp; 
                                    <a id="a_url" class="upload_method">Từ URL</a>
                                    <?php echo $form->hiddenField($model,'upload_method'); ?>
                                </div> 
                                <div style="clear: both;"></div>

                                <div id="image_file">
                                    <?php echo $form->fileField($model,'image_file', array('class' => 'da-custom-file', 'name' => 'browse_file')); ?>
                                </div>
                                <div id="image_url">
                                    <?php echo $form->textField($model,'image_url', array('placeholder' => 'http://domain.com/path/image.jpg')); ?>
                                </div>
                                <?php echo $form->error($model,'image_file');?>
                                <?php echo $form->error($model,'upload_method');?>
                            </div>
                        </div>
                        <div class="da-form-col-3-8">
                            <div class="da-form-item large" id="div_image_preview" style="margin-left: 0;">
                                <?php if($this->action->id == 'update'):?>
                                    <img style="height: 60px; width: auto;" src="<?php echo $model->imageUrlSmall.'?'.uniqid()?>" />
                                    <?php else:?> 
                                    <img id="img_file" style="display: none; height: 60px; width: auto;" /> 
                                    <img id="img_url" style="display: none; height: 60px; width: auto;" />
                                    <?php endif?> 
                            </div>
                        </div>
                    </div>

                    <div class="da-form-row" style="margin-top: 10px;">
                        <div class="da-form-col-6-8">
                            <?php echo $form->labelEx($model,'desc'); ?>
                            <div class="da-form-item large">
                                <span class="formNote">(Description should be 130-160 chars) | <span id="desc_char_count"></span> chars</span>
                                <?php echo $form->textArea($model,'desc', array('maxlength' => 160, 'style' => 'height: 80px')); ?>
                                <?php echo $form->error($model,'desc');?>
                            </div>
                        </div>
                    </div>
                    <div class="da-form-row block_en" style="margin-top: 10px;">
                        <div class="da-form-col-6-8">
                            <?php echo $form->labelEx($model,'desc_en'); ?>
                            <div class="da-form-item large">
                                <span class="formNote">(Description should be 130-160 chars) | <span id="desc_en_char_count"></span> chars</span>
                                <?php echo $form->textArea($model,'desc_en', array('maxlength' => 160, 'style' => 'height: 80px')); ?>
                                <?php echo $form->error($model,'desc_en');?>
                            </div>
                        </div>
                    </div>

                    <div class="da-form-row">
                        <div class="da-form-col-8-8"> 
                            <?php echo $form->labelEx($model,'content'); ?>
                            <div class="da-form-item large">
                                <?php echo $form->textArea($model,'content',array('class'=>'mce_editor', 'style' => 'height: 300px;')); ?>
                                <?php echo $form->error($model,'content');?>
                            </div>
                        </div>
                    </div>

                    <div class="da-form-row block_en">
                        <div class="da-form-col-8-8"> 
                            <?php echo $form->labelEx($model,'content_en'); ?>
                            <div class="da-form-item large">
                                <?php echo $form->textArea($model,'content_en',array('class'=>'mce_editor', 'style' => 'height: 300px;')); ?>
                                <?php echo $form->error($model,'content_en');?>
                            </div>
                        </div>
                    </div>

                    <div class="da-form-row">
                        <div class="da-form-col-4-8">
                            <?php echo $form->labelEx($model,'status'); ?>
                            <div class="da-form-item large">
                                <?php echo $form->dropDownList($model, 'status', Post::model()->getStatusData()); ?>
                                <?php echo $form->error($model,'status');?>
                            </div>
                        </div>
                    </div>

                    <div class="da-form-row">
                        <div class="da-form-col-4-8">
                            <label>Publish time</label>
                            <div class="da-form-item large">
                                <?php echo $form->textField($model, 'created', array('class' => 'datetimepicker')); ?>
                                <?php echo $form->error($model,'created');?>
                            </div>
                        </div>
                        <div class="da-form-col-4-8">
                            Server current time: <b><?php echo MyDateTime::getCurrentTime();?></b> <br/>
                            <?php if($model->isNewRecord):?>If leave blank, the system will retrieve the current time.<?php endif?>
                        </div>
                    </div>

                    <div class="da-form-row">
                        <div class="da-form-col-8-8">
                            <label>Post to FB Page</label>
                            <div class="da-form-item large">
                                <?php echo $form->checkbox($model, 'post_fb', array('style' => 'margin: 8px 0 0;width: auto;')); ?>
                                <?php if($model->fb_posted):?>This Post was posted to facebook page at <?php echo $model->fb_posted?>.<?php endif?>
                            </div>
                        </div>
                    </div>


                    <div class="da-form-row">
                        <div class="da-form-col-3-8">
                            <label></label>
                            <div class="da-form-item">
                                <button type="submit" class="da-button blue">
                                    <?php echo $model->isNewRecord ? 'Add New' : 'Update'?>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <?php $this->endWidget(); ?>


            </div>
        </div>

    </div>
</div>       


<script>
    $(function(){
                
        $('#post-form').submit(function(){
            $('#Post_content').val(tinyMCE.activeEditor.getContent());
        });
        
        $("input.datetimepicker").datetimepicker({ dateFormat: 'yy-mm-dd' });
        $("#Post_title").keyup(function(){
            $('#title_char_count').text($(this).val().length);
        }).keyup();
        $("#Post_title_en").keyup(function(){
            $('#title_en_char_count').text($(this).val().length);
        }).keyup();
        
        $("#Post_desc").keyup(function(){
            $('#desc_char_count').text($(this).val().length);
        }).keyup();
        $("#Post_desc_en").keyup(function(){
            $('#desc_en_char_count').text($(this).val().length);
        }).keyup();


        $('#Post_has_en').click(function(){
            if($('#Post_has_en').attr('checked')){
                $('#post-form .block_en').show();     
            }else{
                $('#post-form .block_en').hide();          
            }
        });

    });   
</script>